package juc.锁.可重入锁.ReentrantLock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Demo2 {

    public static void main(String[] args) {
        /*
        商品
        生产者
        消费者
         */
        Product product = new Product(new ReentrantLock());
        Producer producer = new Producer(product);
        Customer customer = new Customer(product);
        producer.start();
        customer.start();
    }
}

/**
 * 产品
 */
class Product {
    //名称
    String name;
    //价格
    int price;
    //可以生产的标识
    boolean flag = false;
    //锁
    ReentrantLock lock;
    //消费条件
    Condition customerCondition;
    //生产条件
    Condition producerCondition;

    public Product(ReentrantLock lock) {
        this.lock = lock;
        customerCondition = lock.newCondition();
        producerCondition = lock.newCondition();
    }
}

/**
 * 消费者
 */
class Customer extends Thread {
    private Product product;

    public Customer(Product product) {
        this.product = product;
    }

    @Override
    public void run() {
        while (true) {
            try {
                product.lock.lock();
                //先判断产品的标识是否可以消费
                if (product.flag == true) {
                    //消费
                    System.out.println("消费了产品");
                    System.out.println("产品为：" + product.name);
                    System.out.println("价格为：" + product.price);

                    //消费了产品，把标注改为false
                    product.flag = false;

                    //通知在producerCondition上等待的生产者线程进行生产
                    product.producerCondition.signal();
                } else {
                    //消费者线程在customerCondition上等待
                    product.customerCondition.await();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                product.lock.unlock();
            }
        }
    }
}

/**
 * 生产者
 */
class Producer extends Thread {

    private Product product;

    public Producer(Product product) {
        this.product = product;
    }

    @Override
    public void run() {
        int i = 0;
        while (true) {

            try {
                product.lock.lock();
                //产品标识是false没有生产
                if (product.flag == false) {
                    if (i % 2 == 0) {
                        //偶数的时候生产cpu
                        product.name = "CPU";
                        product.price = 2000;
                    } else {
                        //奇数生产内存条
                        product.name = "内存条";
                        product.price = 300;
                    }
                    i++;
                    System.out.println("生产了产品");
                    System.out.println("产品为：" + product.name);
                    System.out.println("价格为：" + product.price);
                    //把产品标识改为true,可以消费
                    product.flag = true;
                    //通知在customerCondition等待的消费者线程进行消费
                    product.customerCondition.signal();
                } else {
                    //已经生产了
                    //生产者线程在producerCondition上等待
                    product.producerCondition.await();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                product.lock.unlock();
            }
        }
    }
}